/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hidromedsa.measure.controller.filesprocessor;

import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author spacerverus
 */
public class FileDialog {

    /**
     *
     */
    JFileChooser fileChooser = new JFileChooser();
    /**
     * Variable set filter for file type
     */
    JFileFilter filter = new JFileFilter();
    /**
     * Variable that stores the separator used in the file path on the computer
     * that runs the program
     */
    protected static String separator;
    /**
     * Variable in which is stored the operating system that runs this program
     */
    protected static String osname;

    /**
     * Method that initializes the class checking the 
     * operating system on which the program runs
     */
    public FileDialog() {
        osname = System.getProperty("os.name");
        separator = (osname.toLowerCase().indexOf("windows") != -1 || osname.toLowerCase().indexOf("Windows") != -1) ? "\\" : "/";
    }

    /**
     * 
     * @param type  File extension to be opened
     * @return String[] which has two records: [0] If the user opening the file accepted [1] The file path
     */
    public String[] Open(String type) {
        String fileArray[] = {"Error", "Null"};
        filter.addType(type);
        filter.setDescription("File ." + type);
        fileChooser.addChoosableFileFilter(filter);
        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File archsel = fileChooser.getSelectedFile();
            /*System.out.println(archsel.getParent());
            System.out.println(archsel.getName());*/
            fileArray[0] = "s";
            fileArray[1] = archsel.getParent() + separator + archsel.getName();
        } else if (status == JFileChooser.CANCEL_OPTION) {
            fileArray[0] = "n";
            System.out.println("Canceled");
        }
        return fileArray;
    }

    /**
     * 
     * @param type Extension of the file you want to save
     * @return String[] has two records: [0] If the user opening the file accepted [1] The file path
     */
    public String[] Save(String type) {
        String fileArray[] = {"Error", "Null"};
        filter.addType(type);
        filter.setDescription("File ." + type);
        fileChooser.addChoosableFileFilter(filter);
        int status = fileChooser.showSaveDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File archsel = fileChooser.getSelectedFile();
            /*System.out.println(archsel.getParent());
            System.out.println(archsel.getName());*/
            fileArray[0] = "s";
            fileArray[1] = archsel.getParent() + separator + archsel.getName() + "." + type;
        } else if (status == JFileChooser.CANCEL_OPTION) {
            fileArray[0] = "n";
            System.out.println("canceled");

        }
        return fileArray;
    }
}
